package com.thrblock.needle.bean;

import engineextend.util.actiontemplet.TempletValue;
import geivcore.UesContext;
import geivcore.Uesi;
import geivcore.engineSys.serialTaskcontroller.SerialTask;
import geivcore.enginedata.canonical.CANExPos;
import geivcore.enginedata.obj.Obj;

import java.util.HashSet;
import java.util.Set;

public class YarnBall implements SerialTask{
	private Set<Needle> crtNeedles = new HashSet<Needle>();
	private Uesi ues = UesContext.getCurrentContext();
	private Obj ball;
	final float normalSpd = 0.7f;
	final float maxSpd = 1.5f;
	final float spdAcc = 0.05f;
	
	private float crtSpd = normalSpd;
	private enum St{NORMAL,ACC,MAX,DEC}
	private St st = St.NORMAL;
	private int regCount = 0;
	public YarnBall() {
		ball = ues.creatObj(Uesi.UIIndex);
		ball.addGLImage(ues.preInitTexture(YarnBall.class.getResourceAsStream("BIGHJ.png")));
		ball.setWidth(180f);
		ball.setHeight(180f);
		//ball.setAllAlph(0.8f);
		ball.setPosition(CANExPos.POS_CENTER);
		
		ues.addSerialTask(this);
	}
	public boolean acupuncture(Needle needle) {
		for(Needle need:crtNeedles) {
			if(need.isCollide(needle)){
				needle.finish(0);
				return true;
			}
		}
		crtNeedles.add(needle);
		return false;
	}
	public void enable() {
		crtNeedles.clear();
		ball.show();
	}
	public void clear() {
		for(Needle needle:crtNeedles) {
			needle.finish(0);
		}
		crtNeedles.clear();
	}
	public void disable() {
		ball.hide();
	}
	public void serial(int clock) {
		if(ball.isPrintable()) {
			switch(st) {
			case ACC:
				if((crtSpd = TempletValue.getStpValue(crtSpd,maxSpd,spdAcc)) == maxSpd) {
					regCount = 0;
					st = St.MAX;
				}
				break;
			case DEC:
				if((crtSpd = TempletValue.getStpValue(crtSpd,normalSpd,spdAcc)) == normalSpd) {
					regCount = 0;
					st = St.NORMAL;
				}
				break;
			case MAX:
				if(regCount < 2 * ues.getFPS()) {
					regCount++;
				} else {
					regCount = 0;
					st = St.DEC;
				}
				break;
			case NORMAL:
				if(regCount < 2 * ues.getFPS()) {
					regCount++;
				} else {
					regCount = 0;
					st = St.ACC;
				}
				break;
			default:
				break;
			}
			ball.setAngle(ball.getAngle() + crtSpd);
			for(Needle need:crtNeedles) {
				need.setAngleSpd(crtSpd);
			}
		}
	}
}
